package net.neiquan.applibrary.numberpicker.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;


import net.neiquan.applibrary.R;
import net.neiquan.applibrary.numberpicker.entity.City;
import net.neiquan.applibrary.numberpicker.entity.Province;
import net.neiquan.applibrary.numberpicker.entity.Region;

import org.haitao.common.utils.FileUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 城市Picker
 *
 * @author zd
 *
 */
public class CityPicker extends LinearLayout {
	/** 滑动控件 */
	private ScrollerNumberPicker provincePicker;
	private ScrollerNumberPicker cityPicker;
	private ScrollerNumberPicker counyPicker;

	/** 临时日期 */
	private int provinceIndex = 0;
	private int cityIndex = 0;
	private int regionIndex = 0;

	/**
	 * 所有省
	 */
	protected ArrayList<String> mProvinceDatas;
	/**
	 * 所有省
	 */
	protected ArrayList<String> mCityDatas;
	/**
	 * 所有省
	 */
	protected ArrayList<String> mRegionDatas;


	List<Province> listP =new ArrayList<Province>();

	public CityPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		// TODO Auto-generated constructor stub
	}

	public CityPicker(Context context) {
		super(context);
		init();
	}


	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
	}


	private void init(){
		LayoutInflater.from(getContext()).inflate(R.layout.city_picker, this);
		// 获取控件引用
		provincePicker = (ScrollerNumberPicker) findViewById(R.id.province);

		cityPicker = (ScrollerNumberPicker) findViewById(R.id.city);
		counyPicker = (ScrollerNumberPicker) findViewById(R.id.couny);

		mProvinceDatas =new ArrayList<String>();
		listP = JSON.parseArray(FileUtils.readFileFromAssets(getContext(), "allcity.json"), Province.class);
		for(Province cityJson:listP){
			mProvinceDatas.add(cityJson.getName());
		}
		provincePicker.setData(mProvinceDatas);
		provincePicker.setDefault(0);

		mCityDatas =new ArrayList<String>();
		List<City> c =listP.get(0).getCitys();
		for(City cityJson:c ){
			mCityDatas.add(cityJson.getName());
		}
		cityPicker.setData(mCityDatas);
		cityPicker.setDefault(0);


		mRegionDatas=new ArrayList<String>();
		List<Region> r =listP.get(0).getCitys().get(0).getRegions();
		for(Region cityJson:r){
			mRegionDatas.add(cityJson.getName());
		}
		counyPicker.setData(mRegionDatas);
		counyPicker.setDefault(0);
		provincePicker.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

			@Override
			public void endSelect(int id, String text) {
				// TODO Auto-generated method stub
				provinceIndex =id;
				mCityDatas =new ArrayList<String>();
				List<City> c2 =listP.get(id).getCitys();
				for(City cityJson:c2 ){
					mCityDatas.add(cityJson.getName());
				}
				cityPicker.setData(mCityDatas);
				cityPicker.setDefault(0);
				cityIndex=0;
				mRegionDatas=new ArrayList<String>();
				List<Region> r =listP.get(provinceIndex).getCitys().get(cityIndex).getRegions();
				for(Region cityJson:r){
					mRegionDatas.add(cityJson.getName());
				}
				counyPicker.setData(mRegionDatas);
				counyPicker.setDefault(0);

			}

			@Override
			public void selecting(int id, String text) {
				// TODO Auto-generated method stub
			}
		});
		cityPicker.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

			@Override
			public void endSelect(int id, String text) {
				// TODO Auto-generated method stub
				mRegionDatas=new ArrayList<String>();
				cityIndex =id;
				List<Region> r =listP.get(provinceIndex).getCitys().get(id).getRegions();
				for(Region cityJson:r){
					mRegionDatas.add(cityJson.getName());
				}
				counyPicker.setData(mRegionDatas);
				counyPicker.setDefault(0);
				regionIndex=0;
			}

			@Override
			public void selecting(int id, String text) {

			}
		});
		counyPicker.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

			@Override
			public void endSelect(int id, String text) {
				regionIndex = id;
			}

			@Override
			public void selecting(int id, String text) {

			}
		});
	}

	public String getPovinceStr() {
		return provincePicker.getSelectedText();
	}
	public String getCityStr() {
		return cityPicker.getSelectedText();
	}
	public String getCounyStr() {
		return counyPicker.getSelectedText();
	}

	public String getPovinceCode() {
		return listP.get(provinceIndex).getCode();
	}
	public String getCityCode() {
		return  listP.get(provinceIndex).getCitys().get(cityIndex).getCode();
	}
	public String getCounyCode() {
		return  listP.get(provinceIndex).getCitys().get(cityIndex).getRegions().get(regionIndex).getCode();
	}

}